package tc.jv.task4;

import tc.jv.task9.IncompatibleVectorSizeException;

public class Vectors
{

	public static Vector add(Vector v1, Vector v2) throws IncompatibleVectorSizeException
	{
		Vector nv = (Vector)v1.clone();
		return nv.sum(v2) ? nv : null;
	}
	
	public static Vector multiply(Vector v, double num)
	{
		Vector nv = (Vector)v.clone();
		return nv.multiply(num) ? nv : null;
	}
	
	public static void sort(Vector v)
	{
		qsort(v, 0, v.size()-1);
	}
	
	private static void qsort(Vector v, int low, int high)
	{
		int i, j;
		double m, temp;
		
		i = low; j = high;
		m = v.get((i + j) / 2);
		do
		{
			while (v.get(i) < m)
				i++;
			while (v.get(j) > m)
				j--;
			if (i <= j)
			{
				temp = v.get(i);
				v.set(i, v.get(j));
				v.set(j, temp);
				i++;
				j--;
			}			
		}
		while (i <= j);
		if (low < j)
			qsort(v, low, j);
		if (i < high)
			qsort(v, i, high);
	}
	
}
